import java.io.IOException;
import java.net.URI;

import org.apache.muse.core.proxy.ProxyHandler;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.Element;
import org.xmlpull.v1.builder.XmlElement;
import org.xmlpull.v1.builder.XmlInfosetBuilder;

import wsdm.ResouceConstants;
import xsul.XmlConstants;
import xsul.http_server.HttpMiniServer;
import xsul.http_server.HttpServerException;
import xsul.invoker.soap_over_http.SoapHttpDynamicInfosetInvoker;
import xsul.processor.DynamicInfosetProcessorException;
import xsul.soap11_util.Soap11Util;
import xsul.soap12_util.Soap12Util;
import xsul.ws_addressing.WsaEndpointReference;
import xsul.ws_addressing.WsaInvoker;
import junit.framework.TestCase;


public class WSDMTest extends TestCase {
	private int port  = 5555;
	@Override
	protected void setUp() throws Exception {
		Thread t = new Thread(new Runnable() {
		
			public void run() {
				try {
					
					MyService service = new MyService();
					HttpMiniServer miniMe = new HttpMiniServer(port);
					service.setServer(miniMe);
					service.start();
					System.out.println("Service started on port "+ port);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
		});
		t.setDaemon(true);
		t.start();
	}
	
	public void testGetResourceDocument()throws Exception{
		Thread.sleep(4000);
		XmlInfosetBuilder builder = XmlConstants.BUILDER;
		XmlElement request = builder.newFragment("http://docs.oasis-open.org/wsrf/rp-2","GetResourcePropertyDocument");
		
		
	
		
		WsaInvoker invoker = new WsaInvoker();
		invoker.setSoapFragrance(Soap11Util.getInstance());
		invoker.setTargetEPR(new WsaEndpointReference(new URI("http://127.0.0.1:"+port)));
		invoker.setDefaultAction(new URI(ResouceConstants._ACTIONS[0]));
		
//		SoapHttpDynamicInfosetInvoker invoker = new SoapHttpDynamicInfosetInvoker("http://127.0.0.1:"+port);
		
		XmlElement response = invoker.invokeMessage(request);
		System.out.println(builder.serializeToString(response));
		
		
//		ProxyHandler handler = ResouceConstants.getHandler("getResourcePropertyDocument");
//		Element body = handler.toXML(new Object[0]);
//		OutputFormat format = new OutputFormat(body.getOwnerDocument());
//		  XMLSerializer output = new XMLSerializer(System.out, format);
//		  output.serialize(body);
		
	}
	
	

}
